<?php
/**
 * Zend Framework Contribution
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://framework.zend.com/license/new-bsd
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@zend.com so we can send you a copy immediately.
 *
 * @category   Zend
 * @package    Zend_Mwb
 * @copyright  Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 * @version    1.0 Table.php 3 mai 2012 Sergio DE VASCONCELOS
 */

require_once('Zend/Mwb/Dom/Abstract.php');

/**
 * MySQL Workbench document
 *
 * @category   Zend
 * @package    Zend_Mwb
 */
class Zend_Mwb_Dom_ForeignKey extends Zend_Mwb_Dom_Abstract
{
	protected $owner;
	protected $columns;
	protected $referencedTable;
	protected $referencedColumns;
	
	public function getOwner() {
		if (is_null($this->owner)) {
			$this->owner = $this->loadOwner();
		}
		return $this->owner;
	}
	public function getReferencedTable() {
		if (is_null($this->referencedTable)) {
			$this->referencedTable = $this->loadReferencedTable();
		}
		return $this->referencedTable;
	}
	public function getColumns() {
		if (is_null($this->columns)) {
			$this->columns = $this->loadColumns();
		}
		return $this->columns;
	}
	public function getReferencedColumns() {
		if (is_null($this->referencedColumns)) {
			$this->referencedColumns = $this->loadReferencedColumns();
		}
		return $this->referencedColumns;
	}
	
	public function parse() {
		$this->xpath = new DOMXPath($this->node->ownerDocument);
		/*
		loadIndex
		*/
		//$this->owner = $this->loadOwner();
		//$this->columns = $this->loadColumns();
		//$this->referencedTable = $this->loadReferencedTable();
		//$this->referencedColumns = $this->loadReferencedColumns();
	}
	public function loadOwner() {
    	$element = $this->xpath->query("link[@key='owner']", $this->node)->item(0);
    	$elementId = $element->textContent;
    	$element = $this->xpath->query("//*[@id='".$elementId."']")->item(0);
    	return $element->getDom();
	}
	public function loadReferencedTable() {
    	$element = $this->xpath->query("link[@key='referencedTable']", $this->node)->item(0);
    	
    	$elementId = $element->textContent;
    	$element = $this->xpath->query("//*[@id='".$elementId."']")->item(0);
    	return $element->getDom();
	}
	public function loadColumns() {
		$this->columns = array();
    	$columns = $this->xpath->query("value[@key='columns']/link", $this->node);
    	foreach($columns as $column) {
	    	$elementId = $column->textContent;
	    	$element = $this->xpath->query("//*[@id='".$elementId."']")->item(0);
			$this->columns[] = $element->getDom();
    	}
    	return $this->columns;
	}
	public function loadReferencedColumns() {
		$this->referencedColumns = array();
    	$columns = $this->xpath->query("value[@key='referencedColumns']/link", $this->node);
    	foreach($columns as $column) {
	    	$elementId = $column->textContent;
	    	$element = $this->xpath->query("//*[@id='".$elementId."']")->item(0);
			$this->referencedColumns[] = $element->getDom();
    	}
    	return $this->referencedColumns;
	}
	
	public function saveReferencedTable() {
	}
	
/*
    protected $_dependentTables = array('BugsProducts');
 
    protected $_referenceMap    = array(
        'Reporter' => array(
            'columns'           => 'reported_by',
            'refTableClass'     => 'Accounts',
            'refColumns'        => 'account_name'
        ),

	<value type="object" struct-name="db.mysql.ForeignKey" id="{01F816F6-3434-4E4E-866E-99F3132FCAA9}" struct-checksum="0x70a8fc40">
	
		<link type="object" struct-name="db.mysql.Table" key="referencedTable">{118E286C-8E04-460F-A94E-25DAF3F78411}</link>
		<value _ptr_="0x5e05660" type="list" content-type="object" content-struct-name="db.Column" key="columns">
			<link type="object">{9BCBC904-09A9-4C9F-8B3D-1AC024FB379D}</link>
		</value>
		<value type="int" key="deferability">0</value>
		<value type="string" key="deleteRule">NO ACTION</value>
		
		<link type="object" struct-name="db.Index" key="index">{464FF655-62EB-4BAA-8ADD-D4FE0F08BCB3}</link>
		<value type="int" key="mandatory">1</value>
		<value type="int" key="many">1</value>
		<value type="int" key="modelOnly">0</value>
		
		<link type="object" struct-name="db.Table" key="owner">{D1F6E044-B27A-4136-A250-0C5866B2F630}</link>
		<value _ptr_="0x5e056f0" type="list" content-type="object" content-struct-name="db.Column" key="referencedColumns">
			<link type="object">{CBEB303F-D730-442F-B753-5CD2975FE0A3}</link>
		</value>
		<value type="int" key="referencedMandatory">1</value>
		<value type="string" key="updateRule">NO ACTION</value>
		<value type="string" key="comment"></value>
		<value type="string" key="name">id_access</value>
		<value type="string" key="oldName">id_access</value>
	</value>
*/

}
